#include<bits/stdc++.h>
#define uint unsigned int
#define ll long long
#define ull unsigned long long
#define mid ((l+r)>>1)
using namespace std;
namespace IO{
    char buff[1<<21],*p1=buff,*p2=buff;
    char getch(){
        return p1==p2&&(p2=((p1=buff)+fread(buff,1,1<<21,stdin)),p1==p2)?EOF:*p1++;
    }
    template<typename T>
    void read(T &x){
        char ch=getch();int fl=1;x=0;
        while(ch>'9'||ch<'0'){if(ch=='-')fl=-1;ch=getch();}
        while(ch<='9'&&ch>='0'){x=x*10+ch-48;ch=getch();}
        x*=fl;
    }
    template<typename T,typename ...Args>
    void read(T &x,Args& ...args){
        read(x);read(args...);
    }
    char obuf[1<<21],*p3=obuf;
    void putch(char ch){
        if(p3-obuf<(1<<21))*p3++=ch;
        else fwrite(obuf,p3-obuf,1,stdout),p3=obuf,*p3++=ch;
    }
    void pc(const char *s){
    	while(*s)putch(*s),s++;
    }
    char ch[100];
    template<typename T>
    void write(T x){
        if(!x)return putch('0');
        if(x<0)putch('-'),x*=-1;
        int top=0;
        while(x)ch[++top]=x%10+48,x/=10;
        while(top)putch(ch[top]),top--;
    }
    template<typename T,typename ...Args>
    void write(T x,Args ...args){
        write(x),putch(' '),write(args...);
    }
    void flush(){fwrite(obuf,p3-obuf,1,stdout);p3=obuf;}
}
using namespace IO;
const int N=5005,mod=1e9+7;
inline int Mod(int x){return x>=mod?x-mod:x;}
int _[N][N],cnt[N];
int C[N][N];
void Init(int n){
	_[0][0]=1;
	for(int i=1;i<=n;i++)
		for(int j=1;j<=n;j++)
			_[i][j]=Mod(1ll*j*_[i-1][j]%mod+_[i-1][j-1]);
	for(int i=0;i<=n;i++)
		for(int j=0;j<=i;j++)
			cnt[i]=Mod(cnt[i]+_[i][j]);
	for(int i=C[0][0]=1;i<=n;i++)
		for(int j=C[i][0]=1;j<=i;j++)
			C[i][j]=Mod(C[i-1][j-1]+C[i-1][j]);
}
int n,m;
int w[15];
int s[N],siz[N];
vector<int>to[N];
int f[1<<10];
void dfs(int x,int fa,int S){
	siz[x]=1;
	for(auto y:to[x]){
		if(y==fa)continue;
		dfs(y,x,S);
		siz[x]+=siz[y];
	}
	if((s[x]&S)==s[x]){
		if(fa==0||(s[fa]&S)!=s[fa])
			for(int i=1;i<=siz[x];i++)
				f[S]=Mod(f[S]+1ll*C[siz[x]][i]*cnt[n-i]%mod);
	}else siz[x]=0;
}
signed main(){
	freopen("water.in","r",stdin);
	freopen("water.out","w",stdout);
	Init(5000);
	read(n,m);
	for(int i=1;i<=m;i++)
		read(w[i]);
	for(int i=1;i<=n;i++){
		int len;read(len);
		while(len--){
			int x;read(x);
			x--;
			s[i]|=(1<<x);
		}
	}
	for(int i=1;i<n;i++){
		int x,y;
		read(x,y);
		to[x].push_back(y);
		to[y].push_back(x);
	}
	for(int i=1;i<(1<<m);i++)
		dfs(1,0,i);
	int ans=0;
	for(int s=1;s<(1<<m);s++){
		for(int t=((s-1)&s);t;t=(t-1)&s)
			f[s]=Mod(f[s]+mod-f[t]);
		int asd=0;
		for(int i=0;i<m;i++)
			if((s>>i)&1)asd++;
		ans=Mod(ans+1ll*w[asd]*f[s]%mod);
	}
	write(ans);
	return flush(),0;
}
